const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const axios = require("axios");
const fs = require("fs");
const CarSchema = new Schema(
  {
    name: {
      type: String,
      required: true,
    },
    price: {
      type: String,
    },

    img: {
      type: String,
    },
  },

  {
    timestamps: true,
  }
);
const Product = mongoose.model("product", CarSchema);
mongoose
  .connect("mongodb://localhost:27017/db-2101", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => {
    console.log("连接成功");
    loadLolData();
  })
  .catch((err) => console.log(err));
async function loadLolData() {
  const products = [];
  const res = await axios.get(
    "https://apps.game.qq.com/daoju/v3/zb/client/goods/GoodsApp.php?output_format=json&biz=lolriotmall&page_no=1&page_size=50&order_by=iRecRank&sort=desc&jsonpName=goodsListApi&opt_type=goods_list&cat_tag_id=101&cat_tag_type=cat&channel=2&_=1618987346923"
  );
  fs.writeFileSync("./lol.json", JSON.stringify(res.data));
  res.data.data.list.forEach((items) => {
    const product = {};
    product.name = items.sMallName;
    product.name = items.sMallName;
    product.price = items.iVipPrice / 100;
    product.img = items.sProfileImg;
    products.push(product);
  });
  await Product.insertMany(products);
  console.log("成功");
}

module.exports = Product;
